#include "SeqList.h"
/*
	** This function will creat a empty array of elemtype,
	* using malloc to get the address of the heap space
	* set the current size = 0
	* @param seqlist seqlist struct point
	* @param capacity seqlist max size
	* @return value null
*/
void SeqList_Init(Seqlist* seqlist, int capacity)
{
	seqlist->elem = (Elemtype*)malloc(sizeof(Elemtype) * capacity);
	assert(seqlist->elem != NULL);
	seqlist->capacity = capacity;
	seqlist->curr_size = 0;
}

int Insert_Seqlist_At_Header(Seqlist* seqlist, Elemtype elem)
{
	Insert_Seqlist_Special(seqlist, 1, elem);
	return 0;
}

int Insert_Seqlist_At_End(Seqlist* seqlist, Elemtype elem)
{
	Insert_Seqlist_Special(seqlist, seqlist->curr_size + 1, elem);
	return 0;
}

int Insert_Seqlist_Special(Seqlist* seqlist, int location, Elemtype elem)
{
	if (seqlist->curr_size == seqlist->capacity) return -2;
	if (location > seqlist->capacity || location < 1) return -1;
	for (int j = seqlist->curr_size; j >= location - 1; j--)
	{
		seqlist->elem[j + 1] = seqlist->elem[j];
	}
	seqlist->elem[location - 1] = elem;
	seqlist->curr_size++;
	return 0;
}

int Erase_seqlist_Special(Seqlist* seqlist, int location)
{
	if (location <1 || location > seqlist->capacity)
	{
		return -2;
	}
	for (int j = location; j < seqlist->curr_size; j++)
	{
		seqlist->elem[j - 1] = seqlist->elem[j];
	}
	seqlist->curr_size--;
	return 0;
}

int Erase_seqlist_Elem(Seqlist* seqlist, Elemtype elem)
{
	Erase_seqlist_Special(seqlist, Find_seqlist_Elem(seqlist, elem));
	return 0;
}

int Change_Seqlist_ByID(Seqlist* seqlist, int location, Elemtype elem)
{
	seqlist->elem[location - 1] = elem;
	return 0;
}

int Change_Seqlist_ByElem(Seqlist* seqlist, Elemtype source, Elemtype elem)
{
	Change_Seqlist_ByID(seqlist, Find_seqlist_Elem(seqlist, source), elem);
	return 0;
}


int Find_seqlist_Elem(Seqlist* seqlist, Elemtype elem)
{
	for (int i = 0; i < seqlist->curr_size; i++)
	{
		if (seqlist->elem[i] == elem)
		{
			return i + 1;
		}
	}
	return 0;
}

Elemtype* Find_seqlist_ByID(Seqlist* seqlist, int location)
{
	if (location <1 || location > seqlist->capacity)
	{
		return NULL;
	}
	return &seqlist->elem[location - 1];
}


void Foreach_Seqlist(Seqlist* seqlist)
{
	printf("******************************\n");
	printf("**   Location ** value **  \n");
	for (int i = 0; i < seqlist->curr_size; i++)
	{
		/// <summary>
		/// this function just use to show int
		/// </summary>
		/// <param name="seqlist"></param>
		
		
		printf("\t%d\t   %d\n", i + 1, seqlist->elem[i]);
		
	}
	printf("******************************\n");
}


